package com.tpolteam.tpol.tpol2019.demo;

import android.app.DownloadManager;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.URLUtil;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.tpolteam.tpol.R;

import androidx.appcompat.app.AppCompatActivity;

public class WebviewSavePicActivity extends AppCompatActivity {


    WebView mWebView;
    //    String  mUrl = "https://devofandroid.blogspot.com/";
    String  mUrl = "https://www.baidu.com/";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_webview_save_pic);

        mWebView = (WebView) findViewById(R.id.web_view);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.setWebViewClient(new WebViewClient());

        registerForContextMenu(mWebView);

        mWebView.loadUrl(mUrl);

        Toast.makeText(WebviewSavePicActivity.this,
                "dir: " + Environment.getDownloadCacheDirectory(), Toast.LENGTH_LONG)
             .show();

    }

    @Override
    public void onCreateContextMenu(ContextMenu contextMenu, View view,
            ContextMenu.ContextMenuInfo contextMenuInfo) {
        super.onCreateContextMenu(contextMenu, view, contextMenuInfo);

        final WebView.HitTestResult webViewHitTestResult = mWebView.getHitTestResult();

        if (webViewHitTestResult.getType() == WebView.HitTestResult.IMAGE_TYPE ||
                webViewHitTestResult.getType() == WebView.HitTestResult.SRC_IMAGE_ANCHOR_TYPE) {

            contextMenu.setHeaderTitle("Download Image...");
            //            contextMenu.setHeaderIcon(R.drawable.ic_menu_gallery);

            contextMenu.add(0, 1, 0, "Click to download")
                       .setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                           @Override
                           public boolean onMenuItemClick(MenuItem menuItem) {

                               String downloadImageURL = webViewHitTestResult.getExtra();

                               if (URLUtil.isValidUrl(downloadImageURL)) {

                                   DownloadManager mDownloadManager = (DownloadManager) getSystemService(
                                           DOWNLOAD_SERVICE);

                                   DownloadManager.Request mRequest = new DownloadManager.Request(
                                           Uri.parse(downloadImageURL));
                                   mRequest.allowScanningByMediaScanner();
                                   mRequest.setNotificationVisibility(
                                           DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

                                   mDownloadManager.enqueue(mRequest);

                                   Toast.makeText(WebviewSavePicActivity.this,
                                           "Image Downloaded Successfully...", Toast.LENGTH_LONG)
                                        .show();
                               } else {
                                   Toast.makeText(WebviewSavePicActivity.this,
                                           "Sorry.. Something Went Wrong...", Toast.LENGTH_LONG)
                                        .show();
                               }
                               return false;
                           }
                       });
        }
    }
}